package com.qianfeng.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

@Controller
public class LoginController {

    @Autowired
    private Producer defaultKaptcha;

    @RequestMapping("/getDashboard")
    public String getDashboard(){
        return "01_dashboard";
    }

    @RequestMapping("/getLogin")
    public String getLogin(){


        return "login";
    }

    @RequestMapping("/login")
    public  String login(Map map, String username, String password, String verifyCode, HttpServletRequest request){
        System.out.println(username);
        System.out.println(password);
        System.out.println(verifyCode);
        //验证码认证
        boolean b = checkVerifyCode(request, verifyCode);
        System.out.println(b);
        if (b!=true){
            map.put("msg","验证码错误");
            return "forward:/getLogin";
        }


        //获取当前实体
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username,password.toCharArray());
        //调用shiro api方法

        try {
            subject.login(token);//如果登录失败会抛出异常
            //token.setRememberMe(true);
        } catch (AuthenticationException ae) {
            ae.printStackTrace();
            //其他登录异常
            map.put("msg","用户名或密码错误");
            return "login";
        }




        return "01_dashboard";
    }

    /**
     * 校验验证码
     * @param request
     * @param verifyCode 前段传过来的验证码
     * @return
     */
    public boolean checkVerifyCode(HttpServletRequest request,String verifyCode){
        String sessionCode = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        System.out.println("session中的验证码是:"+sessionCode);
        System.out.println("前端传送过来的验证码是:"+verifyCode);
        return sessionCode.equals(verifyCode);
    }

    /**
     * 获取验证码
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/getVerifyCode")
    public void getVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取session
        HttpSession session = request.getSession();
        /*设置浏览器缓存页面的时限 0:表示不进行缓存 */
        response.setDateHeader("Expires", 0);
        /*JSP页面禁用缓存*/
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        /*IE5 cache更新说明,已弃用*/
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        /*兼容HTTP/1.0 它的行为与 Cache-Control: no-cache 一致*/
        response.setHeader("Pragma", "no-cache");
        /*内容*/
        response.setContentType("image/jpeg");
        /*生成验证码*/
        String kaptchaText = defaultKaptcha.createText();
        /*将生成的二维码放到session域中*/
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY,kaptchaText);
        /*将生成的二维码封装进图片中然后打印到前端*/
        BufferedImage image = defaultKaptcha.createImage(kaptchaText);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image,"jpg",out);
        out.flush();
        out.close();
    }


}
